package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.mapper.ItemParamMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.pojo.ItemParam;
import com.jt.vo.ItemParamVO;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ItemParamMapper itemParamMapper;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 分页查询
     *      1.手写SQL
     *      2.Mybatis-Plus，IPage接口
     *
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getItemList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<Item>().like(StringUtils.hasLength(pageResult.getQuery()),
                "title", pageResult.getQuery());
        page = itemMapper.selectPage(page, queryWrapper);
        return pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
    }

    /**
     * 更新状态
     * @param item
     */
    @Override
    public void updateItemStatus(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    @Transactional
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }

    @Override
    public void savaItem(ItemVO itemVO) {
        //1.入库商品表
        Item item = itemVO.getItem();
        item.setStatus(true);//默认启用状态
        //要求入库之后返回主键
        //mybatis-plus如果设定了主键自增则会自动的实现数据回显
        itemMapper.insert(item);

        //2.入库商品详情
        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);

        //3.入库商品详情
        ItemParam itemParam = itemVO.getItemParam();
        ItemParamVO[] dynamicArray = itemParam.getDynamicArray();
        ItemParamVO[] staticArray = itemParam.getStaticArray();
        try {
            String dynamicJSON = objectMapper.writeValueAsString(dynamicArray);
            String staticJSON = objectMapper.writeValueAsString(staticArray);
            itemParam.setDynamicArgs(dynamicJSON).setStaticArgs(staticJSON).setId(item.getId());
            itemParamMapper.insert(itemParam);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            //如果程序执行报错，则抛出异常
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteItem(Integer id) {
        itemMapper.deleteById(id);
    }
}
